package com.imu.purse.modules.bitcoin;


import java.io.FileInputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.druid.util.Base64;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

public class TestRpc {
	public static void main(String[] args){

        Map<String, String> headers = new HashMap<String, String>(1);  
        //身份认证  
        headers.put("Content-type", "application/json");
        
        try{
	        JsonRpcHttpClient client = new JsonRpcHttpClient(  
	  
	                 new URL("http://127.0.0.1:8765"),headers);

	        Object result = client.invoke("personal_newAccount",new Object[]{"123456"},Object.class);

	        //Object result1 = client.invoke("movetoaddress",new Object[]{""},Object.class);
			System.out.println("getbalance===================" + result);
			//随机取一个核心钱包的地址
			Random rand = new Random();
			int curSel = rand.nextInt(5);
			System.out.println("curSel===================" + curSel);

			//Object obj = client.invoke("getaccountaddress",new Object[]{"18186130905"},Object.class);
			//System.out.println("getbalance===================" + obj);
			//查看金额
			//checkMoney(client);
			//分析文件
			//parseExcel(client);
			//System.out.println("http://192.168.2.18:12344".hashCode());
			//System.out.println(new BigDecimal("1.3101574587E10"));

		}catch(Throwable e){
        	System.out.println("================" + e.getMessage());
        	/*StringMap obj = Json.decode(e.getMessage());
        	System.out.println(obj);*/
        	//e.printStackTrace();
        }
        /**
         * 转账
         * 1、getinfo   
         * 2、validateaddress  参数为34位地址1GBwgTQXXrdq5efvCdSnnBQQADAGbMTYqf
         * 3、sendtoaddress   参数为34位地址{1GBwgTQXXrdq5efvCdSnnBQQADAGbMTYqf,"8" }
         * */
        
        /**
         * 获取数据
         * 1、getinfo 
         * 2、getblockcount  //区块高度
         * 3、getblockhash
         * 4、getblock
         * 5、gettransaction
         * 7、getaddressesbyaccount
         * 8、入本地库
         * 
         * */
    }

    private static void checkMoney(JsonRpcHttpClient client){
		XSSFWorkbook xssfWorkbook;
		try {
			xssfWorkbook = new XSSFWorkbook(new FileInputStream("d:\\02.xlsx"));
			for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
				XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
				if (xssfSheet == null) {
					continue;
				}
				// Read the Row
				for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow = xssfSheet.getRow(rowNum);
					if (xssfRow != null) {
						XSSFCell address = xssfRow.getCell(0);//昵称
						BigDecimal big = new BigDecimal(address.toString());
						Object obj = client.invoke("getbalance",new Object[]{big.toPlainString()},Object.class);
						if(obj != null){
							Double b = (Double)obj;
							if(b == 0){
								System.out.println("address:" + big + ",money : " + b);
							}
						}
					}
				}
			}

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    private static  void parseExcel(JsonRpcHttpClient client){
		XSSFWorkbook xssfWorkbook;
		try {
			xssfWorkbook = new XSSFWorkbook(new FileInputStream("d:\\01.xlsx"));
			for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
				XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
				if (xssfSheet == null) {
					continue;
				}
				// Read the Row
				for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
					XSSFRow xssfRow = xssfSheet.getRow(rowNum);
					if (xssfRow != null) {
						XSSFCell address = xssfRow.getCell(0);//昵称
						client.invoke("movetoaddress",new Object[]{"18186130905",address.toString(),100,0},Object.class);
					}
				}
			}

		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
